package com.cloudspace.jindun.utils;

import android.content.SharedPreferences;

import com.cloudspace.jindun.JindunApp;

public class AppPreferences  {

    private static AppPreferences instance;


    public synchronized  static  AppPreferences getInstance(){
        if (instance == null){
            instance = new AppPreferences();
        }

        return instance;
    }

    private void  AppPreferences(){

    }

    public static int getInt(String key, int defValue) {
        return getPre().getInt(key, defValue);
    }

    private static SharedPreferences getPre() {
        return JindunApp.getInstance().getSharedPreferences();
    }

    public static void putInt(String key, int value) {
        getPre().edit().putInt(key, value).commit();
    }

    public static float getFloat(String key, float defValue) {
        return getPre().getFloat(key, defValue);
    }

    public static void putFloat(String key, float value) {
        getPre().edit().putFloat(key, value).commit();
    }


    public static long getLong(String key, long defValue) {
        return getPre().getLong(key, defValue);
    }

    /**
     * 以键值对的形式在sharedpreference中保存信息
     *
     * @param key   键
     * @param value 值
     */
    public static void putString(String key, String value) {
        if (getPre() == null || key == null)
            return;
        getPre().edit().putString(key, value).commit();
    }

    public static void putBoolean(String key, Boolean value) {
        if (getPre() == null || key == null)
            return;
        getPre().edit().putBoolean(key, value).commit();
    }

    public static boolean getBoolean(String key, boolean... defValue) {
        if (key == null || getPre() == null)
            return false;
        return defValue.length > 0 ? getPre().getBoolean(key,
                defValue[0]) : getPre().getBoolean(key, false);
    }

    /**
     * 获得配置信息
     *
     * @param key      键
     * @param defValue 找不到键时的默认值
     * @return 若查找到，返回键值；若查找不到但设置了默认值，则返回默认值；其他情况返回空字符串。
     */
    public static String getString(String key, String... defValue) {
        if (key == null || getPre() == null)
            return "";
        return defValue.length > 0 ? getPre().getString(key,
                defValue[0]) : getPre().getString(key, "");
    }
}
